<?php

class AdmController extends Controller
{

	public $layout='//layouts/admin';

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated user to
				'actions'=>array('index','changepass', 'mainpage'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}


	public function actionChangepass()
	{		
		if ( isset($_POST['pass']) && strlen($_POST['pass']) > 0 )
		{
			$id = Yii::app()->user->getId();			
			$model=Users::model()->findByPk((int)$id);

			$model->password = md5( $_POST['pass'] );			
			if($model->save()) {
				$this->redirect(array('index', 'changepass'=>1));
			} else {
				throw new CHttpException(400,'Во время сохранения произошла ошибка. Пароль не изменен');
			}
		}
		$this->render('changepass');
	}

	public function actionMainpage()
	{
		$model=Articles::model()->findByPk( 1 );

		$errors = $this->uploadFiles( 1 );

		if($_POST['delete_attach']) {
			$id_attach = $_POST['id_delete_attach'];
			if ($id_attach) {
				$attach = Uploads::model()->findByPk( $id_attach );
				@unlink('uploads/' . $attach->file_name);

				Uploads::model()->deleteByPk( $id_attach );
			}
		}
		
		if ( isset($_POST['Articles']) )
		{
			$model->attributes=$_POST['Articles'];
			$model->edit_time = time();
			if($model->save()) {

				// save images description
				if ($_POST['alt']) {
					foreach ($_POST['alt'] as $k => $v) {
						if ($k != $_POST['id_delete_attach']) {
							$attach = Uploads::model()->findByPk($k);
							$attach->alt = htmlspecialchars($v);
							$attach->save();
						}
					}
				}

				if ($_POST['save_exit']) {
					$this->redirect(array('index'));
				} elseif ($_POST['save']) {
					$this->redirect(array('mainpage'));
				}
			}
		}
		
		$this->render('mainpage',array(
			'model'=>$model,
			'errors'=>$errors,
		));
	}


	public function actionIndex()
	{
		$this->render('index',
			array( 'changepass' => isset($_GET['changepass']) ? 1 : 0 )
		);
	}

	protected function uploadFiles( $id_owner )
	{
		$errors = array();
		if ($_FILES) {
			foreach ( $_FILES['upload']['name'] as $k => $file )
			{
				if (is_uploaded_file($_FILES['upload']['tmp_name'][$k]))
				{
					$attach = new Uploads();
					$attach->owner_type = 'ARTICLE';
					$attach->id_owner = $id_owner;
					$attach->upload_date = time();
					$attach->original_name = $_FILES['upload']['name'][$k];
					$attach->file_name = $attach->owner_type . '_' . $attach->id_owner . '_' . substr( md5(time()+rand()), 0, 10 );
					$attach->filesize = $_FILES['upload']['size'][$k];

					$tmp = getimagesize( $_FILES['upload']['tmp_name'][$k] );
					if ( $tmp )
					{
						// 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP,
						// 7 = TIFF(intel), 8 = TIFF(motorola), 9 = JPC, 10 = JP2, 11 = JPX
						switch ($tmp[2]) {
							case 1: $ext = 'gif'; break;
							case 2: $ext = 'jpg'; break;
							case 3: $ext = 'png'; break;
							case 6: $ext = 'bmp'; break;
							default : $ext = null; break;
						}
						$attach->file_name .= '.' . $ext;
						$attach->is_image = 1;
						$attach->width = $tmp[0];
						$attach->height = $tmp[1];

						if ( $ext && @move_uploaded_file($_FILES['upload']['tmp_name'][$k], "uploads/".$attach->file_name))
						{
							if ( !$attach->save() ){
								$errors[] = $_FILES['upload']['name'][$k];
							}
						} else {
							$errors[] = $_FILES['upload']['name'][$k];
						}
					} else {
						$errors[] = $_FILES['upload']['name'][$k];
					}
				}
			}
		}
	}

}